#include "DXShader.h"

DXShader::DXShader(LPCTSTR FileName)
{
	m_FileName = FileName;
}

DXShader::~DXShader(void)
{
}

void DXShader::Initialize()
{
	LPD3DXBUFFER buff;
	m_Effect=NULL;

	if FAILED ( D3DXCreateEffectFromFile(
		DXRenderer::Instance()->GetDevice(),
		m_FileName,
		NULL,
		NULL,
		0,
		NULL,
		&m_Effect,
		&buff))
	{
		MessageBox(NULL, (char*)buff->GetBufferPointer(), "Problem a la creation de l'effect ", MB_OK);
	}
}

void DXShader::SetTechnique(LPCSTR Name)
{
	if (FAILED(m_Effect->SetTechnique( m_Effect->GetTechniqueByName(Name) )) )
	{
		WriteConsole("Error setting technique\n");
	}
}

void DXShader::SetVector(D3DXHANDLE ParameterName, D3DXVECTOR4 Vector)
{
	HRESULT sa = m_Effect->SetVector(ParameterName, &Vector);
}

void DXShader::SetMatrix(D3DXHANDLE ParameterName, D3DXMATRIX Matrix)
{
	HRESULT sa = m_Effect->SetMatrix(ParameterName, &Matrix);
}

/*void DXShader::SetTexture(D3DXHANDLE ParameterName, IDirect3DTexture9* Texture)
{
	HRESULT a = m_Effect->SetTexture(ParameterName, Texture );
	if(a == S_OK)
        WriteConsole("Texture chargee correctement\n");
    else
        WriteConsole("Probleme au chargement des textures\n");
}*/

void DXShader::SetTexture(D3DXHANDLE ParameterName, LPDIRECT3DTEXTURE9 Texture)
{
	HRESULT a = m_Effect->SetTexture(ParameterName, Texture );
	if(a == S_OK)
        WriteConsole("Texture chargee correctement\n");
    else
        WriteConsole("Probleme au chargement des textures\n");
}

void DXShader::BeginPass()
{
	unsigned int pPasses = 0;
	HRESULT qa = m_Effect->Begin(&pPasses, 0 );
	if (FAILED(m_Effect->BeginPass( 0 ) ) )
		WriteConsole("Error BeginPass\n");

	HRESULT ssa=m_Effect->CommitChanges();
}

void DXShader::EndPass()
{
	HRESULT a = m_Effect->EndPass();
	a = m_Effect->End();
}
